package com.zcx.qshop.activity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.util.UtilToast;
import com.zcx.qshop.R;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.fragment.ClassifyFragment;
import com.zcx.qshop.fragment.HomeFragment;
import com.zcx.qshop.fragment.MyFragment;
import com.zcx.qshop.fragment.ShopCarFragment;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2015/8/7 0007.
 */
public class NavigationActivity extends QSActivity implements View.OnClickListener{

    public static OnFragmentChangeListener onFragmentChangeListener;

    @BoundView(R.id.navigation_home) private ViewGroup home;

    @BoundView(R.id.navigation_classify) private ViewGroup classify;

    @BoundView(R.id.navigation_shop_car) private ViewGroup shopCar;

    @BoundView(R.id.navigation_my) private ViewGroup my;

    private FragmentManager fragmentManager;

    private HomeFragment homeFragment;

    private ClassifyFragment classifyFragment;

    private ShopCarFragment shopCarFragment;

    private MyFragment myFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_navigation);

        fragmentManager = getFragmentManager();

        home.setOnClickListener(this);

        classify.setOnClickListener(this);

        shopCar.setOnClickListener(this);

        my.setOnClickListener(this);

        onClick(home);

        onFragmentChangeListener = new OnFragmentChangeListener() {

            @Override
            public void onFragmentToHome() {

                onClick(home);

            }

            @Override
            public void onFragmentToShopCar() {

                onClick(shopCar);

            }

        };

    }

    @Override
    public void onClick(View v) {

        setDefault(home, R.mipmap.navigation_home, R.color.dominant_tone_two);

        setDefault(classify, R.mipmap.navigation_classify, R.color.dominant_tone_two);

        setDefault(shopCar, R.mipmap.navigation_shop_car, R.color.dominant_tone_two);

        setDefault(my, R.mipmap.navigation_my, R.color.dominant_tone_two);

        hideFragment(fragmentManager.beginTransaction(), homeFragment);

        hideFragment(fragmentManager.beginTransaction(), classifyFragment);

        hideFragment(fragmentManager.beginTransaction(), shopCarFragment);

        hideFragment(fragmentManager.beginTransaction(), myFragment);

        FragmentTransaction transaction = fragmentManager.beginTransaction();

        switch (v.getId()){

            case R.id.navigation_home:

                setDefault(home, R.mipmap.navigation_home_select, R.color.dominant_tone_one);

                if(homeFragment == null){

                    transaction.add(R.id.navigation_frame_layout, homeFragment = new HomeFragment()).commitAllowingStateLoss();

                }else{

                    transaction.show(homeFragment).commitAllowingStateLoss();

                }

                break;

            case R.id.navigation_classify:

                setDefault(classify, R.mipmap.navigation_classify_select, R.color.dominant_tone_one);

                if(classifyFragment == null){

                    transaction.add(R.id.navigation_frame_layout, classifyFragment = new ClassifyFragment()).commitAllowingStateLoss();

                }else{

                    transaction.show(classifyFragment).commitAllowingStateLoss();

                }

                break;

            case R.id.navigation_shop_car:

                setDefault(shopCar, R.mipmap.navigation_shop_car_select, R.color.dominant_tone_one);

                if(shopCarFragment == null){

                    transaction.add(R.id.navigation_frame_layout, shopCarFragment = new ShopCarFragment()).commitAllowingStateLoss();

                }else{

                    transaction.show(shopCarFragment).commitAllowingStateLoss();

                }

                break;

            case R.id.navigation_my:

                setDefault(my, R.mipmap.navigation_my_select, R.color.dominant_tone_one);

                if(myFragment == null){

                    transaction.add(R.id.navigation_frame_layout, myFragment = new MyFragment()).commitAllowingStateLoss();

                }else{

                    transaction.show(myFragment).commitAllowingStateLoss();

                }

                break;

        }

    }

    private void hideFragment(FragmentTransaction transaction, Fragment fragment){

        if(fragment != null){

            transaction.hide(fragment).commitAllowingStateLoss(); 

        }

    }

    private void setDefault(ViewGroup viewGroup, int resId, int colorId){

        ((ImageView)viewGroup.getChildAt(0)).setImageResource(resId);

        ((TextView)viewGroup.getChildAt(1)).setTextColor(getResources().getColor(colorId));

    }

    @Override
    protected void onDestroy() {

        onFragmentChangeListener = null;

        super.onDestroy();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(keyCode == KeyEvent.KEYCODE_BACK){

            exitBy2Click();

        }

        return false;

    }

    private static Boolean isExit = false;

    private void exitBy2Click() {

        Timer tExit = null;

        if (isExit == false) {

            isExit = true;

            UtilToast.show(this, R.string.app_exit);

            tExit = new Timer();

            tExit.schedule(new TimerTask() {

                @Override
                public void run() {

                    isExit = false;

                }

            }, 2000);

        } else {

            QSApplication.INSTANCE.AppExit(this);

        }

    }

    public interface OnFragmentChangeListener{

        void onFragmentToHome();

        void onFragmentToShopCar();

    }

}
